package collections;

public class LinkedList <T>{
	private LinkedListNode<T> nil;
	
	public LinkedList() {
		nil = new LinkedListNode<T>();
		nil.setNext(nil);
		nil.setPrev(nil);
	}
	
	public LinkedListNode<T> search(T key)
	{
		LinkedListNode<T> node = nil.getNext();
		while(node != nil && !node.getKey().equals(key))
		{
			node = node.getNext();
		}

		return node == nil ? null : node;
	}
	
	public void insert(LinkedListNode<T> node)
	{
		nil.setNext(node);
		node.setPrev(nil);

		LinkedListNode<T> head = nil.getNext();

		node.setNext(head);
		head.setPrev(node);
	}
	
	public void delete(LinkedListNode<T> node)
	{
		LinkedListNode<T> prev = node.getPrev();
		LinkedListNode<T> next = node.getNext();

		if(prev != null)
			prev.setNext(next);
		
		if(next != null)
			next.setPrev(prev);
	}
}
